// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Force Field Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 61 : TYPE_FORCEFIELD

// Function declarations
reserve function ForceField_DebugDraw
reserve function ForceField_DebugSpawn

// Static Values

// Tables

function ForceField_DebugDraw
	temp6 = object.inkEffect
	temp7 = object.alpha
	object.inkEffect = INK_ADD
	temp1 = oscillation
	temp1 <<= 2
	Sin(temp0, temp1)
	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x50
	DrawSpriteFX(0, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(4, FX_INK, object.xpos, object.ypos)
	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x90
	DrawSpriteFX(1, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(3, FX_INK, object.xpos, object.ypos)
	object.alpha = temp0
	object.alpha >>= 4
	object.alpha += 0xC0
	DrawSpriteFX(2, FX_INK, object.xpos, object.ypos)
	object.alpha 	 = temp7
	object.inkEffect = temp6
end function


function ForceField_DebugSpawn
	CreateTempObject(TypeName[Force Field], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 5
	object[tempObjectPos].inkEffect = INK_ADD
end function


event ObjectMain
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -8, -80, 8, 80, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		break
	case 1
		temp0 = object.xpos
		temp0 += 0x400000
		if object[0].xpos > temp0
			object.state = 2
		end if
		break
	case 2
		if object.value1 > 0
			object.value1--
		else
			object.state = 0
		end if
		break
	case 3
		temp0 = object.xpos
		temp0 += 0x160000
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object[currentPlayer].xpos < temp0
				BoxCollisionTest(C_TOUCH, object.entityPos, -256, -80, 8, 80, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					if object[currentPlayer].xvel < 0x20000
						object[currentPlayer].xvel = 0x20000
						object[currentPlayer].speed = 0x20000
						object[currentPlayer].direction = FLIP_NONE
					end if
				end if
			else
				BoxCollisionTest(C_BOX, object.entityPos, -8, -80, 8, 80, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if
		next
		break
	end switch
end event


event ObjectDraw
	temp1 = oscillation
	temp1 <<= 2
	Sin(temp0, temp1)
	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x50
	object.alpha >>= object.value1
	DrawSpriteFX(0, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(4, FX_INK, object.xpos, object.ypos)
	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x90
	object.alpha >>= object.value1
	DrawSpriteFX(1, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(3, FX_INK, object.xpos, object.ypos)
	object.alpha = temp0
	object.alpha >>= 4
	object.alpha += 0xC0
	object.alpha >>= object.value1
	DrawSpriteFX(2, FX_INK, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-8, -72, 2, 144, 382, 266)
	SpriteFrame(-6, -72, 3, 144, 384, 266)
	SpriteFrame(-3, -72, 6, 144, 387, 266)
	SpriteFrame(3, -72, 3, 144, 393, 266)
	SpriteFrame(6, -72, 2, 144, 396, 266)
	foreach (TypeName[Force Field], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 5
		object[arrayPos0].inkEffect = INK_ADD
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].state = 1
			object[arrayPos0].value1 = 10
		else
			object[arrayPos0].priority = PRIORITY_ACTIVE
		end if
	next
	SetTableValue(TypeName[Force Field], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(ForceField_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(ForceField_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	object.drawOrder = 5
	object.inkEffect = INK_ADD
	if object[arrayPos0].propertyValue == 1
		object.value1 = 10
	else
		object.value1 = 0
	end if
	Sin(temp0, 0)
	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x50
	object.alpha >>= object.value1
	DrawSpriteFX(0, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(4, FX_INK, object.xpos, object.ypos)
	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x90
	object.alpha >>= object.value1
	DrawSpriteFX(1, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(3, FX_INK, object.xpos, object.ypos)
	object.alpha = temp0
	object.alpha >>= 4
	object.alpha += 0xC0
	object.alpha >>= object.value1
	DrawSpriteFX(2, FX_INK, object.xpos, object.ypos)
end event

event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-8, -72, 2, 144, 382, 266)
	SpriteFrame(-6, -72, 3, 144, 384, 266)
	SpriteFrame(-3, -72, 6, 144, 387, 266)
	SpriteFrame(3, -72, 3, 144, 393, 266)
	SpriteFrame(6, -72, 2, 144, 396, 266)
end event